---
category: Array
contributors:
    - robinpokorny
created: '2020-12-02'
title: Create cartesian product
updated: '2021-10-13'
---

**JavaScript version**

```js cartesian.js
const cartesian = (...sets) => sets.reduce((acc, set) => acc.flatMap((x) => set.map((y) => [...x, y])), [[]]);
```

**Example**

```js example.js
cartesian([1, 2], [3, 4]); // [ [1, 3], [1, 4], [2, 3], [2, 4] ]

/*
       3       4
   ---------------
1 | [1, 3]  [1, 4]
  |
2 | [2, 3]  [2, 4]

*/
```
